import { showNotify } from 'vant'
import type { AxiosError } from 'axios'

type RequestError = AxiosError<{
  message?: string
  result?: any
  errorMessage?: string
}>
export function errorHandler(error: RequestError): Promise<any> {
  if (error.response) {
    const { data = {}, status, statusText } = error.response
    // 403 无权限
    if (status === 403) {
      showNotify({
        type: 'danger',
        message: (data && data.message) || statusText,
      })
    }
    // 401 未登录/未授权
    else if (status === 401) {
      showNotify({
        type: 'danger',
        message: 'Authorization verification failed',
      })
    }
    else {
      showNotify({
        type: 'danger',
        message: statusText,
      })
    }
  }
  return Promise.reject(error)
}
